unit clsUsuario;

interface

uses Dm, dbtables, biblio;

type
  TUsuario = class(TObject)
  private
    cod_usua: string;
  public
    nome: string;
    senha: string;
    senha2: string;
    acesso: string;
    function Gravar(p_cod_usua: string): Byte;
    function Recuperar(p_cod_usua: string): Byte;
    function Excluir(p_cod_usua: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TUsuario.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM usuario where COD_USUA=:k_cod_usua');
  tq.parambyname('k_cod_usua').asstring := p_cod_usua;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO usuario(COD_USUA,NOME,SENHA,ACESSO,senha2) VALUES(:k_cod_usua,:k_nome,:k_senha,:k_acesso,:k_senha2)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE usuario SET nome=:k_nome,senha=:k_senha,acesso=:k_acesso,senha2=:k_senha2 WHERE cod_usua=:k_cod_usua');
  end;
  tq.parambyname('k_cod_usua').asstring := p_cod_usua;
  tq.parambyname('k_nome').asstring := nome;
  tq.parambyname('k_senha').asstring := criptografar(senha);
  tq.parambyname('k_acesso').asblob := acesso;
  tq.ParamByName('k_senha2').AsString := Criptografar(senha2);
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TUsuario.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM usuario where COD_USUA=:k_cod_usua');
  tq.Params[0].asstring := p_cod_usua;
  tq.open;
  cod_usua := p_cod_usua;
  if tq.eof then begin
    nome := '';
    senha := '';
    Result := 0;
  end else begin
    nome := tq.fieldbyname('nome').asstring;
    senha := DesCriptografar(tq.fieldbyname('senha').asstring);
    acesso := tq.fieldbyname('acesso').value;
    senha2 := DesCriptografar(tq.FieldByName('senha2').AsString);
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TUsuario.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM usuario WHERE cod_usua=:k_cod_usua');
  tq.parambyname('k_cod_usua').asstring := p_cod_usua;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TUsuario.Consultar;

begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from usuario where nome>=:k_nome order by nome');
  p_q.parambyname('k_nome').asstring := p_nome;
end;

end.
